-- RubbishM
-- Created by panyl
-- 垃圾堆模块

module("RubbishM", package.seeall);

-- 捡垃圾
function doEvent(pos)
    local grid = DungeonM.getGridByPos(pos);

    -- 如果不是有效的垃圾堆
    if  grid.type ~= GRID_TYPE_RUBBISH or
        not grid:isOpened() or
        grid.state == GRID_STATE_DISABLE then
        trace("RubbishM", "不是有效的垃圾堆。");
        return false;
    end

    grid:changeState(GRID_STATE_DISABLE);

    local bonus = FormulaM.invoke("CALC_RUBBISH_EVENT_BONUS", grid.bonus);

    -- 执行建筑奖励
    BuildingBonusM.doBuildingBonus(bonus, "rubbish_bonus");

    DungeonM.addAction({ ["cmd"] = "rubbish", ["pos"] = pos, });

    if DungeonM.isCostRound(grid) then
        -- 抛出一个回合事件
        EventMgr.fire(event.COMBAT_ROUND, grid:getPos());
    end

    -- 抛出事件
    EventMgr.fire(event.RUBISH_EVENT, { ["pos"] = pos, ["bonus"] = bonus,});

    -- 通知建筑事件完成
    EventMgr.fire(event.BUILDING_EVENT_DONE, {["pos"] = pos,});

    -- action已经添加
    return true, true;
end
